package com.kkb.kkbportal.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.kkbcommon.exception.Asserts;
import com.kkb.kkbportal.dao.BrowsingHistoryDao;
import com.kkb.kkbportal.domain.BrowsingHistory;
import com.kkb.kkbportal.service.BrowsingHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName BrowsingHistoryServiceImpl
 * @Description 历史浏览
 * @Author hxf
 * @Date 2021/8/14
 **/
@Service
public class BrowsingHistoryServiceImpl extends ServiceImpl<BrowsingHistoryDao, BrowsingHistory> implements BrowsingHistoryService {
    /**
     * 增加或者更新一条历史记录
     *
     * @param browsingHistory
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void add(BrowsingHistory browsingHistory) {
        //同一用户访问同一个项目，则只存储最新的一个数据
        LambdaQueryWrapper<BrowsingHistory> eq = new LambdaQueryWrapper<BrowsingHistory>()
                .eq(BrowsingHistory::getUserId, browsingHistory.getUserId())
                .eq(BrowsingHistory::getProjectId, browsingHistory.getProjectId());
        List<BrowsingHistory> list = baseMapper.selectList(eq);
        if (CollUtil.isNotEmpty(list)) {
            int i = baseMapper.update(browsingHistory, eq);
            if (i == 0) {
                Asserts.fail("历史记录更新失败");
            }
        } else {
            int i = baseMapper.insert(browsingHistory);
            if (i == 0) {
                Asserts.fail("历史记录存入失败");
            }
        }
    }

    /**
     * 取得传入的用户ID的所有项目浏览记录
     *
     * @param userId
     * @return List<BrowsingHistory>
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public List<BrowsingHistory> list(Long userId) {
        BrowsingHistory browsingHistory = new BrowsingHistory();
        browsingHistory.setUserId(userId);
        List<BrowsingHistory> list = lambdaQuery().eq(BrowsingHistory::getUserId, userId).orderByDesc(BrowsingHistory::getUpdateTime).list();
        return list;
    }

    /**
     * 通过用户ID分页获取历史记录
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return PageInfo<BrowsingHistory>
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public PageInfo<BrowsingHistory> listByUserId(Long userId, int pageNum, int pageSize) {
        PageInfo<BrowsingHistory> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            lambdaQuery().eq(BrowsingHistory::getUserId, userId).orderByDesc(BrowsingHistory::getUpdateTime).list();
        });
        return pageInfo;
    }

    /**
     * 在用户改变项目报名状态（或者说是项目状态）后也需要改变项目在历史表中的状态
     *
     * @param browsingHistory
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void updateProjectStatus(BrowsingHistory browsingHistory) {
        int i = baseMapper.update(browsingHistory, new LambdaQueryWrapper<BrowsingHistory>()
                .eq(BrowsingHistory::getUserId, browsingHistory.getUserId())
                .eq(BrowsingHistory::getProjectId, browsingHistory.getProjectId())
        );
        if (i == 0) {
            Asserts.fail("历史记录项目状态更新失败");
        }
    }
}
